package com.teamwork.ZuccEcho.Controller;

import com.alibaba.fastjson.JSON;
import com.teamwork.ZuccEcho.Entity.Course;
import com.teamwork.ZuccEcho.Entity.Student;
import com.teamwork.ZuccEcho.Entity.Teacher;
import com.teamwork.ZuccEcho.Repository.ClassApiRepository;
import com.teamwork.ZuccEcho.Result.ExceptionMsg;
import com.teamwork.ZuccEcho.Result.Response;
import com.teamwork.ZuccEcho.Result.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/course")
public class ClassApiController {
    protected Response result(ExceptionMsg msg){
        return new Response(msg);
    }
    protected Response result(){
        return new Response();
    }

    @Autowired
    private ClassApiRepository classApiRepository; //导入课程的repository
    @Autowired
    RestTemplateBuilder restTemplateBuilder; //导入restTemplateBuilder

    //TODO 创建课程
    @RequestMapping(value = "/add/v1/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData createCourse(@RequestBody Course course){//传入实体，实体在postman中通过json传入
        Course tmp = classApiRepository.findById(course.getC_id());
        if(tmp!=null){//存在该课程
            return new ResponseData(ExceptionMsg.FAILED,"已存在该课程，不能创建");
        }else{//不存在该课程，可以创建
            //查找是否有该老师
            int cnt=classApiRepository.findTeacher(course.getTeacher().getTch_id());
            if(cnt>0){//存在该老师，可以添加
                course.setC_createdate(new Date(System.currentTimeMillis()));//添加创建课程的日期
                Course course1 = classApiRepository.save(course);
                //测试查看添加的课程的任课老师的信息是否是Teacher中的对应的记录
                List<Course> list = classApiRepository.findAll();
                for (Course c : list) {
                    System.out.println(c.getTeacher().getTch_name());//查看老师名字，以验证传入的tch_id是否对应Teacher表中的指定老师
                }
                return new ResponseData(ExceptionMsg.SUCCESS,course1);
            }else{//不存在该老师
                return new ResponseData(ExceptionMsg.FAILED,"不存在所指定的老师，不能创建");
            }
        }
    }

    //TODO 查看指定老师的课程
    @RequestMapping(value = "/get/{tch_id}/v1/", method = RequestMethod.GET)
    public ResponseData getCourses(@PathVariable("tch_id") String tch_id){//参数url指定的老师id
        List<Course> list = new ArrayList<>(classApiRepository.findCoursesByTeacher(tch_id));//查找指定老师的课程
        if(list.size()>0){
            for (Course c : list) {
                c.getStudents();//懒加载，先使用得到学生列表
                c.getQuestionnaireList();//懒加载，先使用得到问卷列表
            }
            String strJSON = JSON.toJSONString(list);//将要返回的课程列表转为json字符串，返回给前端
            return new ResponseData(ExceptionMsg.SUCCESS,strJSON);
        }else{
            return new ResponseData(ExceptionMsg.FAILED,"不存在指定老师教授的课程");
        }
    }

    //TODO 查找学生的所有课程
    @RequestMapping(value = "/stuget/{stu_id}/v1/", method = RequestMethod.GET)
    public ResponseData getStuCourses(@PathVariable("stu_id") String stu_id){
        List<Course> courses=classApiRepository.getCoursesByStu(stu_id);
        if(courses.size()>0){
            String strJSON = JSON.toJSONString(courses);
            return new ResponseData(ExceptionMsg.SUCCESS,strJSON);
        }else{
            return new ResponseData(ExceptionMsg.FAILED,"该学生没有课");
        }
    }

    //TODO 关闭课程
    @RequestMapping(value = "/close/{c_id}/v1/", method = RequestMethod.POST)
    @ResponseBody
    public Response closeCourse(@PathVariable("c_id") long c_id){
        Course course=classApiRepository.findById(c_id);
        if("已开课".equals(course.getC_status())){//只有已开课的课程可以关闭
            classApiRepository.closeCourse(new Date(System.currentTimeMillis()),c_id);//更改课程状态为“已结课”和时间
            return result(ExceptionMsg.SUCCESS);
        }else{
            return new ResponseData(ExceptionMsg.IllegalStatus,"实体类型不符合要求");
        }
    }

    //TODO 注销（删除）课程
    @RequestMapping(value = "/cancel/{c_id}/v1/", method = RequestMethod.POST)
    @ResponseBody
    public Response cancelCourse(@PathVariable("c_id") long c_id){
        Course course=classApiRepository.findById(c_id);
        if("已结课".equals(course.getC_status())){//只有已结课的课程可以注销
            classApiRepository.cancelCourse(new Date(System.currentTimeMillis()),c_id);//更改课程状态为“已注销”和时间
            return result(ExceptionMsg.SUCCESS);
        }else{
            return new ResponseData(ExceptionMsg.IllegalStatus,"实体类型不符合要求");
        }
    }

    //TODO 上课学生管理——添加学生
    @RequestMapping(value = "/student/add/{c_id}/v1/", method = RequestMethod.POST)
    @ResponseBody
    public Response addStudent(@PathVariable("c_id") long c_id,@RequestBody Student student){
        int cnt = classApiRepository.countStudent(c_id, student.getStu_id());
        if(cnt==0){
            classApiRepository.addStudent(c_id,student.getStu_id());
            return result(ExceptionMsg.SUCCESS);
        }else{
            return new ResponseData(ExceptionMsg.FAILED,"该课程已存在该学生，无法添加");
        }
    }

    //TODO 上课学生管理——移除学生（相当于学生退课）
    @RequestMapping(value = "/student/remove/{c_id}/v1/", method = RequestMethod.POST)
    @ResponseBody
    public Response removeStudent(@PathVariable("c_id") long c_id,@RequestBody Student student){
        int cnt = classApiRepository.countStudent(c_id, student.getStu_id());
        if(cnt>0){
            classApiRepository.removeStudent(c_id,student.getStu_id());
            return result(ExceptionMsg.SUCCESS);
        }else{
            return new ResponseData(ExceptionMsg.FAILED,"该课程不存在该学生，无法移除");
        }
    }
}
